function[w] = perco(X, t, maxEpoches)
% Calculates the perceptron weight vector w for the training set (X; t)
% using the training method described in the lectures.
%
%   INPUT
%   X...........The training data; the training vectors are stored as
%               columns. The function takes care of converting them into
%               homogeneous coordinates.
%   t...........A vector containing the targets (class labels). Must have
%               the same size as the number of columns in X.
%   maxEpoches..A number, setting the maximum count of iterations; if
%               reached without finding a solution an Exception is thrown.
%   OUTPUT
%   w...........The weights in homogenous coordinates (so the first value
%               in this vector is the negative theta). The last value tells
%               if the algorithm converged within maxEpoches

    N = size(X, 2);
    if (N ~= size(t))
        err = MException('EFMEGrB4:sizeMismatch', 'The number of columns in X and the size of t must match');
        throw(err);
    end
    % init w, adapt X and t
    X = [ones(1, N); X];
    w = zeros(size(X, 1), 1);
    t(t == 0) = -1;
    for epoche = 1 : maxEpoches
        misclassified = false;
        for i = 1 : N
            temp = X(:, i) * t(i);
            if w' * temp <= 0
                misclassified = true;
                w = w + temp;
            end
        end
        if ~misclassified
            w = [w; true];
            return;
        end
    end
    w = [w; false];
end
